package com.zhong.beautifulimageapp.fragments.ui;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zhong.beautifulimageapp.R;
import com.zhong.beautifulimageapp.adapter.TopicListViewAdapter;
import com.zhong.beautifulimageapp.model.CommonVariable;
import com.zhong.beautifulimageapp.model.TopicDetail;
import com.zhong.beautifulimageapp.model.TopicHeader;
import com.zhong.beautifulimageapp.network.GetContentFromNetwork;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class TopicDetailActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.topic_detail_back)
    ImageView mTopicDetailBack;
    @BindView(R.id.topic_detail_classify)
    TextView mTopicDetailClassify;
    @BindView(R.id.topic_detail_share)
    ImageView mTopicDetailShare;
    @BindView(R.id.topic_detail_TopicDetailAndReplay)
    ListView mTopicDetailTopicDetailAndReplay;
    @BindView(R.id.topic_detail_Toolbar)
    Toolbar mTopicDetailToolbar;
    @BindView(R.id.topic_detail_refreshLayout)
    SwipeRefreshLayout mTopicDetailRefreshLayout;
    private GetContentFromNetwork mNetwork;
    private TopicDetailHeaderView mHeaderView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic_detail);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    private void initView() {
        mTopicDetailRefreshLayout.setOnRefreshListener(this);
        mHeaderView = new TopicDetailHeaderView(this, mTopicDetailTopicDetailAndReplay);
        setSupportActionBar(mTopicDetailToolbar);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(CommonVariable.baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        mNetwork = retrofit.create(GetContentFromNetwork.class);
    }

    private void getData() {
        if (getIntent().hasExtra("TopicId")) {
            String id = getIntent().getStringExtra("TopicId");
            getTopicDetailData(id);
        }
    }

    private void getTopicDetailData(String id) {
        mNetwork.getTopicDetailContent(id, "true", null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<TopicDetail>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull TopicDetail detail) {
                        if (detail.isSuccess()) {
                            mTopicDetailClassify.setText("话题");
                            TopicHeader header = new TopicHeader();
                            TopicHeader.AuthorData data = new TopicHeader.AuthorData();
                            data.setAvatar_url(detail.getData().getAuthor().getAvatar_url());
                            data.setLoginname(detail.getData().getAuthor().getLoginname());
                            header.setAuthor(data);
                            header.setAuthor_id(detail.getData().getAuthor_id());
                            header.setContent(detail.getData().getContent());
                            header.setCreate_at(detail.getData().getCreate_at());
                            header.setGood(detail.getData().isGood());
                            header.setId(detail.getData().getId());
                            header.setIs_collect(detail.getData().isIs_collect());
                            header.setLast_reply_at(detail.getData().getLast_reply_at());
                            header.setReply_count(detail.getData().getReply_count());
                            header.setTab(detail.getData().getTab());
                            header.setTitle(detail.getData().getTitle());
                            header.setVisit_count(detail.getData().getVisit_count());
                            header.setTop(detail.getData().isTop());
                            mHeaderView.updateView(header);
                            TopicListViewAdapter adapter = new TopicListViewAdapter(TopicDetailActivity.this);
                            adapter.setReplayList(detail.getData().getReplies());
                            mTopicDetailTopicDetailAndReplay.setAdapter(adapter);
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(TopicDetailActivity.this, "网络不给力", Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void onRefresh() {
        if (mTopicDetailRefreshLayout.isRefreshing()) {
            mTopicDetailRefreshLayout.setRefreshing(false);
        }
    }
}
